package com.leapp.sk.view.im

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import cc.vv.lklibrary.log.LogOperate
import com.leapp.sk.R
import com.leapp.sk.bean.EmojiconEntity
import com.leapp.sk.bean.EmojiconGroupEntity
import com.leapp.sk.view.im.EaseEmojiconScrollTabBar.EaseScrollTabBarItemClickListener
/**表情控件封装
 * @author  chaohaipeng  on 2018/7/13.
 */
class SKEmojiconMenu :SKEmojiconMenuBase{
    /**
     * 上下文
     */
    var mContext: Context? = null
    /**
     * 普通表情列数
     */
    private var emojiconColumns: Int = 0
    /**
     * 大表情列数
     */
    private var bigEmojiconColumns: Int = 0
    private var tabBar: EaseEmojiconScrollTabBar? = null
    private var indicatorView: EaseEmojiconIndicatorView? = null
    private var pagerView: SKEmojiconPagerView? = null
    private val emojiconGroupList = mutableListOf<EmojiconGroupEntity>()
    constructor(context: Context) : super(context) {
        mContext = context
    }


    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        init(context,attrs )
    }

    /**
     * 初始化表情布局
     */
    private fun init(context: Context, attrs: AttributeSet?) {
      val view=  LayoutInflater.from(context).inflate(R.layout.ease_widget_emojicon, this)
        val ta = context.obtainStyledAttributes(attrs, R.styleable.EaseEmojiconMenu)
        val defaultColumns = 7
        emojiconColumns = ta.getInt(R.styleable.EaseEmojiconMenu_emojiconColumns, defaultColumns)
        val defaultBigColumns = 4
        bigEmojiconColumns = ta.getInt(R.styleable.EaseEmojiconMenu_bigEmojiconRows, defaultBigColumns)
        ta.recycle()

        pagerView = view.findViewById(R.id.pager_view)
        indicatorView = view.findViewById(R.id.idv_ewe_indicator_view)
        tabBar = view.findViewById(R.id.tab_bar)

    }

    /**
     * 初始化表情组数据
     */
    fun init(groupEntities: List<EmojiconGroupEntity>?) {
        if (groupEntities == null || groupEntities.size == 0) {
            return
        }
        for (groupEntity in groupEntities) {
            emojiconGroupList.add(groupEntity)
            tabBar?.addTab(groupEntity.icon)
        }
        pagerView?.setPagerViewListener(EmojiconPagerViewListener())
        pagerView?.init(groupEntities, emojiconColumns, bigEmojiconColumns)
        tabBar?.setTabBarItemClickListener(object :EaseScrollTabBarItemClickListener {
            override fun onItemClick(position: Int) {
                pagerView?.setGroupPostion(position)
            }
        })

    }


    /**
     * 表情添加到表情组中
     * @param groupEntity
     */
    fun addEmojiconGroup(groupEntity: EmojiconGroupEntity) {
        emojiconGroupList.add(groupEntity)
        pagerView?.addEmojiconGroup(groupEntity, true)
        tabBar?.addTab(groupEntity.icon)
    }

    /**
     * 添加表情到表情组列表中
     * @param groupEntitieList
     */
    fun addEmojiconGroup(groupEntitieList: List<EmojiconGroupEntity>) {
        for (i in groupEntitieList.indices) {
            val groupEntity = groupEntitieList[i]
            emojiconGroupList.add(groupEntity)
            pagerView?.addEmojiconGroup(groupEntity, if (i == groupEntitieList.size - 1) true else false)
            tabBar?.addTab(groupEntity.icon)
        }

    }

    /**
     * 移除表情组
     * @param position
     */
    fun removeEmojiconGroup(position: Int) {
        emojiconGroupList.removeAt(position)
        pagerView?.removeEmojiconGroup(position)
        tabBar?.removeTab(position)
    }

    fun setTabBarVisibility(isVisible: Boolean) {
        if (!isVisible) {
            tabBar?.visibility=View.GONE
        } else {
            tabBar?.visibility=View.VISIBLE
        }
    }


    private inner class EmojiconPagerViewListener : SKEmojiconPagerView.EaseEmojiconPagerViewListener {

        override fun onPagerViewInited(groupMaxPageSize: Int, firstGroupPageSize: Int) {
            indicatorView?.init(groupMaxPageSize)
            indicatorView?.updateIndicator(firstGroupPageSize)
            tabBar?.selectedTo(0)
        }

        override fun onGroupPositionChanged(groupPosition: Int, pagerSizeOfGroup: Int) {
            indicatorView?.updateIndicator(pagerSizeOfGroup)
            tabBar?.selectedTo(groupPosition)
        }

        override fun onGroupInnerPagePostionChanged(oldPosition: Int, newPosition: Int) {
            indicatorView?.selectTo(oldPosition, newPosition)
        }

        override fun onGroupPagePostionChangedTo(position: Int) {
            indicatorView?.selectTo(position)
        }

        override fun onGroupMaxPageSizeChanged(maxCount: Int) {
            indicatorView?.updateIndicator(maxCount)
        }

        override fun onDeleteImageClicked() {
                listener?.onDeleteImageClicked()
        }

        override fun onExpressionClicked(emojicon: EmojiconEntity) {
                listener?.onExpressionClicked(emojicon)


        }

    }
}